﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System;
using System.Threading.Tasks;
using System.Timers;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Configuration;

namespace SampleWebSite.Controllers
{
    public class XiaoMiQuickBuyController : Controller
    {
        //
        // GET: /XiaoMiQuickBuy/

        public ActionResult Index()
        {
            return View();
        }

        public void XiaomiGoLink()
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://t.hd.xiaomi.com/r/?_a=payment_check");
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
            req.Method = "POST";
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            req.Headers.Add("Accept-Language:zh-CN,zh;q=0.8");
            req.KeepAlive = true;
            req.Headers.Add("Keep-Alive: 300");
            req.Referer = "https://account.xiaomi.com/pass/serviceLogin";

            req.ContentType = "application/x-www-form-urlencoded";

            //String Username = "username";
            //String PassWord = "Password";

            //StreamWriter sw = new StreamWriter(req.GetRequestStream());
            //sw.Write("user=hotyi2003@gmail.com&sid=eshop&callback=http%3A%2F%2Forder.xiaomi.com%2Flogin%2Fcallback%3Ffollowup%3Dhttp%253A%252F%252Fwww.xiaomi.com%252F%26sign%3DMjk4NmVlYzNiOWY5YTkwMjExNzg5MjkyNzE3ZmIxOGM4YTBiMDk1Mg%2C%2C&_sign=f%2FKB8SAfmaLZ0s28kcLtBSmZlEM%3D&_json=true&pwd=yi43520798929");

            //sw.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();


            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();

            foreach (Cookie cook in response.Cookies)
            {
                tmp += "\n" + cook.Name + ": " + cook.Value;
            }


            Response.Write(tmp);
            Response.End();

        }


        public void XiaomiLogin()
        {

            //HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://account.xiaomi.com/pass/serviceLogin");
           

      

         

            //HttpWebResponse response1 = (HttpWebResponse)req.GetResponse();


            //StreamReader reader1 = new StreamReader(response1.GetResponseStream());
            //string tmp1 = reader1.ReadToEnd();

            //foreach (Cookie cook in response1.Cookies)
            //{
            //    tmp1 += "\n" + cook.Name + ": " + cook.Value;
            //}
            //var cookie = response1.Headers["set-cookie"];



            var postData =
                "passToken=&user=hotyi2003@gmail.com&pwd=yi43520798929&callback=https%3a%2f%2faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2bgLdeyOsdMhwV0Xg%3d&auto=true";

            var buffer = Encoding.Default.GetBytes(postData);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://account.xiaomi.com/pass/serviceLoginAuth2");
            req.ServicePoint.Expect100Continue = true;
            req.Method = "POST";
            req.Timeout = 100000;
            req.KeepAlive = true;
            req.ReadWriteTimeout = 30000;
            req.Accept = "text/html, application/xhtml+xml, */*";
            req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            req.Referer = "https://account.xiaomi.com/pass/serviceLogin";
            req.AllowAutoRedirect = false;
            req.Headers[HttpRequestHeader.Cookie] =
                //  "deviceId=wb_1ee0afce-ac7a-4b5d-a55d-6940dfa395dd; uLocale=zh_CN; JSESSIONID=aaaeJj5Pk6Kro4Wj71Jhu; Hm_lvt_c64922fe907004774c797344b7063851=1382498520,1382498523,1382498598,1382498651; Hm_lpvt_c64922fe907004774c797344b7063851=1382506683";

                "deviceId=wb_1649b9db-5d87-4161-9429-73b8da8cf673; domain=account.xiaomi.com; path=/; expires=Mon, 10-Nov-2081 09:55:37 GMT,uLocale=zh_CN; path=/; expires=Thu, 07-Nov-2013 06:41:30 GMT,JSESSIONID=aaaZdeAYD_ow3dzm-cKhu; path=/";

            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = buffer.Length;


            //StreamWriter sw = new StreamWriter(req.GetRequestStream());
            //sw.Write("user=hotyi2003@gmail.com&sid=eshop&callback=http%3A%2F%2Forder.xiaomi.com%2Flogin%2Fcallback%3Ffollowup%3Dhttp%253A%252F%252Fwww.xiaomi.com%252F%26sign%3DMjk4NmVlYzNiOWY5YTkwMjExNzg5MjkyNzE3ZmIxOGM4YTBiMDk1Mg%2C%2C&_sign=KKkRvCpZoDC+gLdeyOsdMhwV0Xg=&_json=true&pwd=yi43520798929");
            //sw.Write(
             //      "passToken=&user=hotyi2003@gmail.com&pwd=yi43520798929&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2BgLdeyOsdMhwV0Xg%3D");

            

            req.GetRequestStream().Write(buffer, 0, buffer.Length);


            HttpWebResponse response = (HttpWebResponse)req.GetResponse();


            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();

            foreach (Cookie cook in response.Cookies)
            {
                tmp += "\n" + cook.Name + ": " + cook.Value;
            }


            Response.Write(tmp);
            Response.End();

        }



        //public ActionResult Login()
        //{
        //    string username = txtUserName.Text;
        //    string password = txtPwd.Text;

        //    HttpItem itemSign = new HttpItem()          //获取_sign
        //    {
        //        URL = "https://account.xiaomi.com/pass/serviceLogin",
        //    };
        //    HttpHelper helperSign = new HttpHelper();
        //    HttpResult resultSign = helperSign.GetHtml(itemSign);
        //    // <input type="hidden" name="_sign" value="KKkRzxzZoDC+gLdeyOszxzV0Xg=">
        //    string _sign = System.Uri.EscapeDataString(Regex.Match(resultSign.Html, "name=\"_sign\" value=\"(.+?)\"").Groups[1].Value);
        //    cookies = resultSign.Cookie;
        //    HttpItem itemLogin = new HttpItem()         //登陆Post
        //    {
        //        URL = "https://account.xiaomi.com/pass/serviceLoginAuth2",
        //        Method = "POST",
        //        Cookie = cookies,
        //        Referer = "https://account.xiaomi.com/pass/serviceLogin",
        //        ContentType = "application/x-www-form-urlencoded",
        //        Postdata = "passToken=&user=" + username + "&pwd=" + password + "&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2BgLdeyOsdMhwV0Xg%3D&auto=true"

        //    };
        //    HttpHelper helperLogin = new HttpHelper();
        //    HttpResult resultLogin = helperLogin.GetHtml(itemLogin);

        //    if (resultLogin.Html.Contains("小米帐户 - 登录"))
        //    {
        //        AppendText(username + "登陆失败\n");
        //        return;
        //    }
        //    AppendText(username + "登陆成功");
        //    cookies += ";" + resultLogin.Cookie;
        //    // AppendText(cookies);
        //}


    }

    public class HttpResult
    {
    }

    public class HttpHelper
    {
    }

    public class HttpItem
    {
        public string URL { get; set; }
    }
}
